package org.hzy.test1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.swing.text.StyledEditorKit.BoldAction;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hzy.entity.Blobs;
import org.hzy.entity.Dept;
import org.hzy.entity.Emp;

public class HQLtest2 {

	/**
	 * @param args
	 */

	static Configuration con = new Configuration().configure();
	static SessionFactory fac = con.buildSessionFactory();
	static Session sess = fac.getCurrentSession();

	static void ts1(String hql) {
		Transaction tran = sess.beginTransaction();

		Query quer = sess.createQuery(hql);

		List<Object[]> list = quer.list();
		System.out.println(list.size());

		Iterator it = list.iterator();

		while (it.hasNext()) {
			Object[] obj = (Object[]) it.next();

			Dept d = (Dept) obj[0];
			Emp e = (Emp) obj[0];
			System.out.println(d.getDeptno() + "   " + e.getEmpno());

			/*
			 * Object obj=it.next(); Dept d=(Dept) obj;
			 * 
			 * System.out.println(d.getDeptno());
			 */
		}

		tran.commit();
	}

	static void ts2(String hql) {
		Transaction tran = sess.beginTransaction();

		Query quer = sess.createQuery(hql);

		List<Object[]> list = quer.list();

		Iterator it = list.iterator();

		while (it.hasNext()) {
			Object[] obj = (Object[]) it.next();
			System.out.println(obj[0] + "   " + obj[1]);

		}
		tran.commit();
	}

	static void ts3(String hql) {
		Transaction tran = sess.beginTransaction();

		Query quer = sess.createQuery(hql);

		List<Object[]> list = quer.list();

		Iterator it = list.iterator();

		while (it.hasNext()) {
			Dept d = (Dept) it.next();
			System.out.println(d.getDeptno());

		}
		tran.commit();
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// ts1("from Dept d,Emp e where d.deptno=e.dept.deptno order by d.deptno");

		// ts1("from Dept d left join d.emps e where d.deptno=e.dept.deptno order by d.deptno");

		// ts1("from Dept d left join fetch d.emps e where d.deptno=e.dept.deptno order by d.deptno");

		// ts1("from Emp e left join e.dept d where d.deptno=e.dept.deptno order by d.deptno");

		// ts2("select d.deptno,e.empno from Dept d,Emp e where d.deptno=e.dept.deptno order by d.deptno");

		// ts1("from Dept d where d.deptno=true");

		// ts3("from Dept d where d.emps.size>0");
		// //ts3("from Dept d where size(d.emps)>0");

		// ts3("from Dept d where maxindex(d.emps)>7900"); //max(emp.empno)>7900

		// ts3("from Dept d where minindex(d.emps)>7900");//min(emp.empno)>5
		// 对于有序的集合

		// size，elements，indices，minindex，maxindex，minelement，maxelement — 只能在
		// where 子句中使用。

		// ts3("from Dept d where minelement(d.emps)>5"); //min(emp.empno)>5

		// ts3("from Dept d where d.emps['7369'].empno=7369");

/*		Blobs blo = new Blobs();
		blo.setBid(1);
		blo.setBname("one");
		
		Blob bs = null;

		try { // 将图片读进输入流
			FileInputStream file = new FileInputStream("c:\\a.jpg");

			// 转成Blob类型 
			bs=Hibernate.createBlob(file);

		} catch (IOException e) { // TODO Auto-generated catch block
			e.printStackTrace();
		}
		blo.setBimage(bs);
		Transaction tr = sess.beginTransaction();
		sess.save(blo);
		tr.commit();*/
		
		
		Transaction tr = sess.beginTransaction();
		Blobs bs = (Blobs) sess.get(Blobs.class, 1);

		try {
			// 从数据库中要读取出来
			InputStream is = bs.getBphoto().getBinaryStream();
			// 在把写到一个图片格式的文件里
			FileOutputStream fos = new FileOutputStream("c:"+File.separator+"xxy1.jpg");
			
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = is.read(buffer)) != -1) {
				fos.write(buffer, 0, len);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		tr.commit();
	}
}
